const app = (() => {
    const {
        arrayToMap,
    } = rx
    return new BaseValidateVue({
        el: '#app',
        data: function () {
            return {
                formData: new BaseServerInfo(),
                houseMap: new Map(),
                positionMap: new Map(),
                areaMap: new Map(),
                frameMap: new Map(),
                customerMap: new Map(),
                ruleList: [
                    new ValidateObject({
                        ruleName: 'server_number_validate',
                        validate: this.serverNumberValidate,
                    }),
                ],
                originalServiceNumber: _.cloneDeep(forward.getData().serverNumber),
            }
        },
        methods: {
            createValidateList() {
                return this.ruleList
            },
            async submitForm() {
                if (!await this.$validator.validateAll()) {
                    await rxPrompt.dangerMsg('输入存在错误！')
                    return
                }
                //  提交数据
                if ((await baseServerInfoApi.update(this.formData)).code === 200) {
                    await rxPrompt.successMsg('修改成功')
                    forward('/basic-data/admin_servers')
                } else {
                    await rxPrompt.dangerMsg('修改失败')
                }
            },
            async serverNumberValidate(val) {
                const {position = ''} = this.formData
                if (position === '' || val === '' || val === this.originalServiceNumber) {
                    return true
                }
                return await baseServerInfoApi.selectServiceNumByPositionId(position, val)
            },
        },
        watch: {
            async ['formData.houseId'](val) {
                this.areaMap = null
                if (val) {
                    this.areaMap = arrayToMap(await baseIdcHouseAreaApi.selectListByParam({houseId: val}), 'id', 'areaName')
                }
            },
            async ['formData.area'](val) {
                this.frameMap = null
                if (val) {
                    this.frameMap = arrayToMap(await baseFrameInfoApi.selectListByParam({houseArea: val}), 'id', 'frameName')
                }
            },
            async ['formData.frame'](val) {
                this.positionMap = null
                if (val) {
                    this.positionMap = arrayToMap(await basePositionApi.selectListByParam({frameId: val}), 'id', 'name')
                }
            },
        },
        async mounted() {
            this.formData = forward.getData()
            await Promise.all([
                this.customerMap = arrayToMap(await baseCustomerApi.selectList(), ({id}) => id, ({unitName}) => unitName),
                this.houseMap = arrayToMap(await baseHouseInfoApi.selectList(), ({id}) => id, ({houseName}) => houseName),
            ])
        },
    })
})()
